package com.yanqu.road.server.cmd.banquet.cross;

import com.yanqu.road.entity.banquet.UserCrossBanquetData;
import com.yanqu.road.entity.banquet.cross.CrossBanquetMember;
import com.yanqu.road.entity.player.PlayerState;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.banquet.BanquetModule;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.CrossBanquetMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_BANQUET_CROSS_MEMBER_SYNC, desc = "加入宴会后的同步")
public class SyncCrossBanquetMemberCmd extends AbstractCommand {

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		CrossBanquetProto.SyncCrossBanquetMemberMsg reqMsg = CrossBanquetProto.SyncCrossBanquetMemberMsg.parseFrom(packet.getBytes());
		String banquetId = "";
		List<CrossBanquetMember> memberList = new ArrayList<>();
		for (CrossBanquetProto.SyncCrossBanquetMemberTemp temp : reqMsg.getMemberListList()){
			CrossBanquetMgr.syncBanquetMember(temp);
			banquetId = temp.getBanquetId();

			CrossBanquetMember member = new CrossBanquetMember();
			member.setBanquetId(temp.getBanquetId());
			member.setGiftId(temp.getGiftId());
			member.setIntegral(temp.getIntegral());
			member.setPopularity(temp.getPopularity());
			member.setUserId(temp.getUserId());
			member.setPos(temp.getPos());
			member.setRobot(temp.getRobot());
			member.setRobotProcessId(temp.getRobotProcessId());
			member.setRobotId(temp.getRobotId());
			member.setUserBaseInfo(PlayerBasePb.parseBaseUserInfo(temp.getUserBaseInfo()));
			memberList.add(member);
		}
		// 批量同步
		UserCrossBanquetData banquetData = CrossBanquetMgr.getBanquet(banquetId);


		BanquetProto.BanquetMemberSync.Builder msg = BanquetProto.BanquetMemberSync.newBuilder();
		msg.setBanquetId(banquetData.getBanquetId());
		msg.setPopularity(banquetData.getPopularity());
		msg.setCount(banquetData.getUserCount());
		//改成跨服宴会不同步了
		if(BanquetConfigMgr.isCrossUserBanquet(banquetData.getType())){
			for (CrossBanquetMember member : memberList) {
				BanquetProto.BanquetMemberTemp.Builder temp = BanquetProto.BanquetMemberTemp.newBuilder();
				temp.setUserId(member.getUserId());
				temp.setPos(member.getPos());
				temp.setPopularity(member.getPopularity());
				temp.setIsRobot(member.isRobot());
				temp.setRobotId(member.getRobotId());
				temp.setGiftId(member.getGiftId());
				PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(member.getUserBaseInfo());
				temp.setPlayerBaseData(baseMsg);
				msg.addMemberList(temp);
			}
		}

		for(long userId : banquetData.getSceneBanquetUser()){
			GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(userId);
			if(onlinePlayer != null && onlinePlayer.getState() == PlayerState.ONLINE){
				Long pingTime = onlinePlayer.getLastPingTime();
				Long nowTime = System.currentTimeMillis();
				if(nowTime - pingTime < 5*1000){//ping小于5秒才同步
					onlinePlayer.sendPacket(Protocol.U_BANQUET_CROSS_MEMBER_SYNC, msg);
					continue;
				}
			}
			CrossBanquetMgr.removeAllBanquetScene(userId);
		}

		GamePlayer owenr = GamePlayerMgr.getOnlinePlayer(banquetData.getUserId());
		if(owenr != null){
			CrossBanquetMgr.syncSelfCrossBanquet(owenr,banquetData);
		}

		if(BanquetConfigMgr.isSendCrossBanquetPopRewardPoint(banquetData.getUserCount())){
			List<Long> playerIdList = GamePlayerMgr.getAllOnlinePlayerId();
			for(Long userId : playerIdList){
				GamePlayer player1 = GamePlayerMgr.getOnlinePlayer(userId);
				player1.getModule(BanquetModule.class).crossBanquetPopRewardSync(banquetData);
			}
		}

	}

	@Override
	public void sendData(GamePlayer player) {

	}

}
